#ifndef _TAIIC_ISP_MODEL_H_
#define _TAIIC_ISP_MODEL_H_
#include <cstdio>
#include "videodev2.h"
#include <condition_variable>
#include <mutex>
#include <taiic_opencv.h>
using namespace std;
/*-------------isp related 宏定义-----------------*/
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define FMT_NUM_PLANES 1

#define BUFFER_COUNT 3

#ifdef ANDROID
#define CAPTURE_RAW_PATH "/data"

#else
#define CAPTURE_RAW_PATH "/tmp"
#define DEFAULT_CAPTURE_RAW_PATH "/tmp/capture_image"
#endif
#define CAPTURE_CNT_FILENAME ".capture_cnt"
#define IQFILE_PATH_MAX_LEN 256

//switch stats related params
static const char *main_scene = "normal";
static const char *stats_day="day";
static const char *stats_night="night";

static int switch_rst=1;

static int flag_day=0; //0->off,1->on
static int flag_night=0;
static char sns_entity_name[64];

#define ORI_MODEL_C 48 // 48 frames 输入lm模型
//送入LM模型的图像宽度和高度
#define ORI_WIDTH 112 
#define ORI_HEIGHT 112

static sigset_t mask;

struct buffer {
    void *start;
    size_t length;
    int export_fd;
    int sequence;
};

static demo_context_t second_ctx;
static demo_context_t third_ctx;
static demo_context_t fourth_ctx;


enum TEST_CTL_TYPE {
    TEST_CTL_TYPE_DEFAULT,
    TEST_CTL_TYPE_REPEAT_INIT_PREPARE_START_STOP_DEINIT,
    TEST_CTL_TYPE_REPEAT_START_STOP,
    TEST_CTL_TYPE_REPEAT_PREPARE_START_STOP,
};

static demo_context_t main_ctx = {
        .out_file = {'\0'},
        .dev_name = {'\0'},
        .dev_name2 = {'\0'},
        .dev_name3 = {'\0'},
        .dev_name4 = {'\0'},
        .sns_name = {'\0'},
        .dev_using = 1,
        .width = 320,
        .height = 320,
        .format = V4L2_PIX_FMT_NV12,
        .fd = -1,
        .buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
        .buffers = NULL,
        .n_buffers = 0,
        .frame_count = 51,
        .fp = NULL,
        .aiq_ctx = NULL,
        .camgroup_ctx = NULL,
		.ir_ctx = NULL,
        .vop = 0,
        .rkaiq = 1,
        .writeFile = 1, 
        .writeFileSync = 0, // .writeFileSync = 0,
        .pponeframe = 0,
        .hdrmode = 0,
        .limit_range = 0,
        .fd_pp_input = -1,
        .fd_isp_mp = -1,
        .buffers_mp = NULL,
        .outputCnt = 51, //output frame count
        .skipCnt = 0,
        .yuv_dir_path = {'\0'},
        ._is_yuv_dir_exist = false,
        .capture_yuv_num = 0,
        .is_capture_yuv = false,
        .ctl_type = TEST_CTL_TYPE_DEFAULT,
        .iqpath = {'\0'},
        .orppath = {'\0'},
        .orpRawW = 0,
        .orpRawH = 0,
        .orpRawFmt = {'\0'},
        .isOrp = false,
        .orpStop = false,
        .orpStopped = false,
        .camGroup = false,

    };
    #define IQ_PATH "/etc/iqfiles"
    #define DEV_NAME "/dev/video11"   //or "/oem/usr/share/iqfiles"
    #define OUT_FILE_NAME "/data/out.yuv"
   //20240420 add 
//    struct termios oldt;
   static int silent;
   
   static bool _if_quit = false;
   static bool _quit_done = false;
   static RK_U32 loop_isp = 0;


   #define SAVE_PIC_CNT 50
   static pthread_t tid_image_cap;
   static pthread_t tid_spp_send_img;//spp_send_img_thread_func()

   void restore_terminal_settings(void);
   void disable_terminal_return(void);
   
   void test_update_iqfile(const demo_context_t* demo_ctx);
   void test_imgproc(const demo_context_t* demo_ctx);
   int xioctl(int fh, int request, void *arg);
   void flush_and_sync(FILE *fp);
   static int set_ae_onoff(const rk_aiq_sys_ctx_t* ctx, bool onoff);
   static void print_af_stats(rk_aiq_isp_stats_t *stats_ref);
 
   
   //add by mtd at 2024/05/09
   static char * mode_cmd=NULL;
   //v3
   static char mode_rst[100]={0};
   //状态线程控制
  static mutex mtx_stats;
  static condition_variable cv_stats; // 条件变量
  static bool start_stats = false;

  //音频获取控制线程
  static bool ai_start = false;
  static mutex mtx_ai;
  static condition_variable cv_ai; // 条件变量

  //启动握把控制线程
  static mutex mtx_woba;
  static condition_variable cv_woba;
  static bool start_woba = false;

  //到上位机的上传控制flg，发送图像时，停止电量和模式状态的上传
  static bool state_send_flag= true;//默认进行模式状态和电池电量的发送
  static mutex mtx_state;
  static condition_variable cv_state;

  //控制isp图像采集
  static mutex mtx_vi;
  static condition_variable cv_vi; // 条件变量
  static bool start_vi = false;
  
  //电台控制标志位,默认电台模式
  static bool diantai_flag=true;

//2024.12.04 add by mtd
#define RK_SMART_IR_AUTO_IRLED true
typedef struct sample_smartIr_s
{
    pthread_t tid;
    bool tquit;
    const rk_aiq_sys_ctx_t *aiq_ctx;
    bool started;
    const char *ir_cut_v4ldev;
    const char *ir_v4ldev;
    rk_smart_ir_ctx_t *ir_ctx;
    rk_smart_ir_params_t ir_configs;
} sample_smartIr_t;

static sample_smartIr_t g_sample_smartIr_ctx;

//2024.12.05 add by mtd
static void enableIrCutter(bool on);
void switch_to_day();
void switch_to_night();
char *get_dev_name(demo_context_t *ctx);
char *get_sensor_name(demo_context_t *ctx);
static void errno_exit(demo_context_t *ctx, const char *s);
static int read_frame(demo_context_t *ctx, int flg);
static void stop_capturing(demo_context_t *ctx);
static void start_capturing(demo_context_t *ctx);
static void start_capturing_pp_oneframe(demo_context_t *ctx);
static void uninit_device(demo_context_t *ctx);
static void uninit_device_pp_oneframe(demo_context_t *ctx);
static void init_mmap(int pp_onframe, demo_context_t *ctx);
static void init_input_dmabuf_oneframe(demo_context_t *ctx);
static void init_device(demo_context_t *ctx);
static void init_device_pp_oneframe(demo_context_t *ctx);
static void close_device(demo_context_t *ctx);
static void open_device(demo_context_t *ctx);
static void close_device_pp_oneframe(demo_context_t *ctx);
static void open_device_pp_oneframe(demo_context_t *ctx);
static void uninit_device_pp_onframe(demo_context_t *ctx);
static void deinit(demo_context_t *ctx);
static void signal_handle(int signo);
static int query_ae_state(const rk_aiq_sys_ctx_t *ctx);
static void set_af_manual_meascfg(const rk_aiq_sys_ctx_t *ctx);
static void *stats_thread(void *args);
static void test_tuning_api(demo_context_t *ctx);
static void load_ir_configs(demo_context_t *ctx);
static void rkisp_routine(demo_context_t *ctx, int flg);
static void sample_smartIr_start(demo_context_t *ctx);
void release_buffer(void *addr);
static void taiic_isp_release();
static void *taiic_isp_image_capture_func(void *args);
static demo_context_t *g_main_ctx = NULL, *g_second_ctx = NULL, *g_third_ctx = NULL, *g_fourth_ctx = NULL;

//soc:320*320*1.5*50
#define VI_SOC_SIZE 7680000 
#define VI_SOC_SIZE_MODEL 7372800 //48 frames

/*---20250420 add by mtd---*/
//控制socket数据发送线程
static mutex mtx_socket;
static condition_variable cv_socket; //条件变量
static bool start_socket = false;



#endif



